import { _decorator, Button, Component, director, game, Node, tween, UIOpacity, UITransform, v3 } from 'cc';
import { GameEventType, GameOverType } from './Event/EnumDefine';
import { EventManager } from './Event/EventManager';
import { DataConfig } from './Config/DataConfig';
import { SoundManager, SoundType } from './SoundManager';
import Common from './Config/Common';
import SHtml from './SHtml/SHtml';
import { PlayerAction } from './SHtml/PrintComponent';
const { ccclass, property } = _decorator;

/** 结束界面，目前没有资源，临时的 */
@ccclass('GameOver')
export class GameOver extends Component {

    @property(Node)
    flyStartNode: Node; // 道具开始飞的起始位置的节点

    @property(Node)
    mask: Node = null;

    @property(Node)
    showNode: Node = null;

    @property(Node)
    failNode: Node = null;

    @property(Node)
    successNode: Node = null;

    @property(Node)
    btnDown: Node = null;

    @property(Node)
    downLoad: Node = null;

    @property(Node)
    blockEvent: Node = null;

    isAutoDown: boolean = false;

    dt: number = 0;

    onLoad() {
        EventManager.instance.registerEvent(GameEventType.Game_Over, this.onShowGameOver, this);

        this.onScreen();
        EventManager.instance.registerEvent(GameEventType.Screen_Change, this.onScreen, this);
    }

    protected start(): void {
        // 是否隐藏下载按钮
        this.btnDown.active = !SHtml.instance.is_hide_download();
    }

    protected update(dt: number): void {
        this.dt = dt;
    }

    onScreen() {
        let size = Common.instance.getScreenSize();
        this.node.getComponent(UITransform).setContentSize(size);
        this.mask.getComponent(UITransform).setContentSize(size);
        this.blockEvent.getComponent(UITransform).setContentSize(size);

        // btnDown
        let downUI = this.btnDown.getComponent(UITransform);
        let downW = downUI.contentSize.width;
        let downH = downUI.contentSize.height;
        this.btnDown.setPosition(size.width * 0.5 - downW * 0.5 - 10, -size.height * 0.5 + downH * 0.5 + 20);
    }

    onShowGameOver(overType: GameOverType) {
        DataConfig.isGameStop = true;
        SoundManager.instance.stopAllSounds();

        let param = { isDie: false, isWin: false };
        if (overType === GameOverType.GameWin) {
            param = { isDie: false, isWin: true };
        }
        else if (overType === GameOverType.PlayerDie) {
            param = { isDie: true, isWin: false };
        }
        else {
            param = { isDie: false, isWin: false };
        }

        SHtml.instance.pStuck(this.dt, 1, true); // 结束 上报时间
        SHtml.instance.pTouchFinish();  // 结束 点击完结
        SHtml.instance.pGameFinish(param.isWin);
        EventManager.instance.triggerEvent(GameEventType.Player_Status_Update, { isDie: param.isDie });

        if (param.isWin) {
            this.playAniShow(true);
        }
        else {
            this.scheduleOnce(this.playAniShow.bind(this, false), 1);
        }
    }

    /** 弹窗动画 */
    playAniShow(success: boolean) {
        this.failNode.active = !success;
        this.successNode.active = success;

        this.mask.active = true;
        this.mask.getComponent(UIOpacity).opacity = 0;

        // 播放背景遮罩动画
        let funcMask = () => {
            tween(this.mask.getComponent(UIOpacity)).to(0.245, { opacity: 200 }).start();
        };
        // 开启拦截
        let funcBefore = () => {
            this.blockEvent.active = true;
        };
        // 显示下载按钮
        let funcAfter = () => {
            // 关闭拦截
            this.blockEvent.active = false;

            // 是否弹出下载按钮
            let isHideDownload = SHtml.instance.is_hide_download();
            this.downLoad.active = !isHideDownload;
            if (this.downLoad.active) {
                this.downLoad.getComponent(UIOpacity).opacity = 0;
                tween(this.downLoad.getComponent(UIOpacity)).to(0.245, { opacity: 255 }).delay(1.0).call(() => {
                    if (this.isAutoDown) {
                        SHtml.instance.download();
                        SHtml.instance.pActionbar(PlayerAction.automatic_jump);
                    }
                }).start();
            }
        };

        // 自动弹出商店页面
        this.isAutoDown = SHtml.instance.check_auto_download();
        if (success) {
            this.playAniSuccess(funcMask, funcBefore, funcAfter);
        }
        else {
            this.playAniFail(funcMask, funcBefore, funcAfter);
        }
    }

    playAniSuccess(funcMask: Function, funcBefore: Function, funcAfter: Function) {
        funcBefore();
        // 播放弹窗主体动画
        this.showNode.active = true;
        this.successNode.children.forEach((item) => { item.active = item.name == 'icon'; });
        this.successNode.setScale(0.35, 0.35);
        this.successNode.setPosition(this.flyStartNode.position);
        tween(this.successNode).delay(0.25).to(0.75, { position: v3(0, 50), scale: v3(1, 1) }).call(() => {
            funcMask();
            this.successNode.children.forEach((item) => { item.active = true; });
        }).delay(1.0).call(() => {
            funcAfter();
        }).start();
    }

    playAniFail(funcMask: Function, funcBefore: Function, funcAfter: Function) {
        funcMask();
        funcBefore();
        // 播放弹窗主体动画
        this.showNode.active = true;
        this.showNode.scale = v3(0.5, 0.5);
        this.showNode.getComponent(UIOpacity).opacity = 0;
        tween(this.showNode.getComponent(UIOpacity)).to(0.13, { opacity: 255 }).start();
        tween(this.showNode)
            .to(0.15, { scale: v3(1.05, 1.05) }, { easing: 'cubicOut' })
            .to(0.15, { scale: v3(0.98, 0.98) }, { easing: 'sineInOut' })
            .to(0.15, { scale: v3(1.00, 1.00) }, { easing: 'sineInOut' })
            .call(funcAfter)
            .start();
    }

    onClickDownload() {
        console.log('ClickDown');
        this.isAutoDown = false;
        SHtml.instance.download();
        SHtml.instance.pActionbar(PlayerAction.download);
    }
}

